/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.forms;

import java.sql.Timestamp;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.ValidatorForm;

public class BaseCommunityForm extends ValidatorForm{
	public Long keyId;
	public ActionErrors errors;
	protected static final FastDateFormat ISO_DATE = FastDateFormat.getInstance("yyyy-MM-dd");
	public ActionErrors getErrors() {
		return errors;
	}
	public void setErrors(ActionErrors errors) {
		this.errors = errors;
	}
	public Long getKeyId() {
		return keyId;
	}
	public void setKeyId(Long keyId) {
		this.keyId = keyId;
	}
	
	/**
	 * This method creates a timestamp from the year, month, and day provided
	 * Hours, minutes, and seconds are set to 0
	 * @param year
	 * @param month
	 * @param day
	 * @return Timestamp
	 */
	public Timestamp getDateTimestamp(String year, String month, String day){
//		try{
//			this.ISO_DATE.p
//			return (Timestamp)(this.ISO_DATE.parseObject(year+"-"+month+"-"+day));
//		}
//		catch (Exception e){
//			System.out.println("ERROR: error parsing date: communityBaseForm");
//			return null;
//		}
		try
		{
			return Timestamp.valueOf(year + "-" + month + "-" + day + " 00:00:00");
		}
		catch (Exception e){
			System.out.println("ERROR: error parsing date: communityBaseForm");
			return null;
		}		
	}
	
	

	/**
	 * This method creates a timestamp from the year, month, day, hours, and minutes provided
	 * Seconds are set to 0
	 * @param year
	 * @param month
	 * @param day
	 * @param hours
	 * @param minutes
	 * @return
	 */
	public Timestamp getDateTimestamp(String year, String month, String day, String hour, String minutes, String amOrPm){
		try
		{
			if (amOrPm.equals("Am or Pm"))
				return null;
			if (amOrPm.equals("pm"))
			{
				// add 12 hours to the hours
				int iHour = Integer.parseInt(hour);
				iHour += 12;
				hour = String.valueOf(iHour);
			}
					
			return Timestamp.valueOf(year+"-"+month+"-"+day+" "+hour+":"+minutes+":00");
		}
		catch (Exception e){
			System.out.println("ERROR: error parsing date: communityBaseForm");
			return null;
		}		
	}
	  
    /**
     * This method checks if there is any data saved in the given fields
     * @return true if all fields are missing
     */
    public boolean isDateEmpty(String day, String month, String year) {
    	boolean b = false;
    	if (day.equals("Day")&&month.equals("Month")&&
    			year.equals("Year"))
    	{
    		b = true;
    	}    	
    	return b;
    }
    
    
    /**
     * This method checks if there is any data saved in the given fields
     * @return true if all fields are missing
     */
    public boolean isTimeEmpty(String hours, String minutes, String amOrPm) {
    	boolean b = false;
    	if (hours.equals("Hour")&&minutes.equals("Minutes")&&
    			amOrPm.equals("Am or Pm"))
    	{
    		b = true;
    	}    	
    	return b;
    }
}
